package com.atguigu.serviceedu.service.impl;

import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduCourseDescription;
import com.atguigu.serviceedu.entity.frontVo.CourseFrontVo;
import com.atguigu.serviceedu.entity.frontVo.CourseWebVo;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.entity.vo.CourseQuery;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseDescriptionService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jyd
 * @since 2022-03-20
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //获取简介表的service，执行插入操作
    @Autowired
    private EduCourseDescriptionService descriptionService;
    //获取章节和小节的service
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;

    @Override
    public String saveCourse(CourseInfoVo courseInfoVo) {

        //添加课程信息
        //获取course数据 先new一个course对象 利用BeanUtils.copyProperties()加载数据
        //BeanUtils.copyProperties()第一个参数为value 第二个参数为set对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0){
            throw new GuLiException(20001,"添加失败");
        }
        //添加课程简介
        //课程表和描述表是一对一的关系 需要有关联关系
        //获取课程id
        String cid = eduCourse.getId();
        //先new一个课程简介对象
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        //设置描述id就是课程id
        courseDescription.setId(cid);
        boolean save = descriptionService.save(courseDescription);
        if(!save){
            throw new GuLiException(20001,"描述添加失败");
        }
        return cid;
    }

    @Override
    public CourseInfoVo getCourseById(String courseId) {
        //查询课程信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //创建封装对象
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //进行封装
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //查询简介信息
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        //进行封装
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        //修改简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(eduCourseDescription);
    }

    //获取课程发布信息
    @Override
    public CoursePublishVo getCoursePublishVo(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(courseId);
        return coursePublishVo;
    }

    @Override
    public void selectCourse(Page<EduCourse> coursePage, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if(courseQuery == null){
            IPage<EduCourse> courseIPage = baseMapper.selectPage(coursePage, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }

        baseMapper.selectPage(coursePage, queryWrapper);
    }

    @Override
    public void removeCourse(String courseId) {
        //根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        //根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //根据课程id删除课程描述
        descriptionService.removeById(courseId);
        //删除课程
        int result = baseMapper.deleteById(courseId);
        if(result < 1){
            throw new GuLiException(20001,"删除课程列表信息失败！");
        }
    }

    @Cacheable(value = "course",key = "'selectcourseList'")
    @Override
    public List<EduCourse> courseList() {
        //查询课程前八条热门课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        //根据id降序排序
        courseQueryWrapper.orderByDesc("id");
        //查询八条
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(courseQueryWrapper);
        return courseList;
    }

    //课程条件分页查询
    @Override
    public Map<String,Object> getPageList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //条件查询
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            if(courseFrontVo.getBuyCountSort().equals("1")){
                wrapper.orderByAsc("buy_count");
            }else{
                wrapper.orderByDesc("buy_count");
            }
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            if(courseFrontVo.getGmtCreateSort().equals("1")){
                wrapper.orderByAsc("gmt_create");
            }else{
                wrapper.orderByDesc("gmt_create");
            }
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            if(courseFrontVo.getPriceSort().equals("1")){
                wrapper.orderByAsc("price");
            }else{
                wrapper.orderByDesc("price");
            }
        }

        baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    //根据课程id获取课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.selectInfoWebById(courseId);
        return courseWebVo;
    }


}
